package com.example.tilitili.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tilitili.mapper.SeckillGoodsMapper;
import com.example.tilitili.pojo.Goods;
import com.example.tilitili.pojo.SeckillGoods;
import com.example.tilitili.service.ISeckillGoodsService;
import com.example.tilitili.vo.GoodsVo;
import com.example.tilitili.vo.RespBean;
import com.example.tilitili.vo.RespBeanEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liu
 * @since 2022-01-17
 */
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {
    @Resource
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 秒杀商品上架
     * @param
     * @return
     */
    @Override
    @Transactional
    public RespBean doseckillground(GoodsVo goodsVo,HttpServletRequest request, HttpServletResponse response) {
        Long Id=goodsVo.getId();
        BigDecimal seckillPrice=goodsVo.getSeckillPrice();
        Integer stockCount=goodsVo.getStockCount();
        Date startDate=goodsVo.getStartDate();
        Date endDate=goodsVo.getEndDate();
        //log.info("{}",mobile)
        if(stockCount>goodsVo.getGoodsStock()){
            return RespBean.error(RespBeanEnum.SECKILL_STOCK);
        }
        if(seckillPrice.compareTo(goodsVo.getGoodsPrice())==1){
            return RespBean.error(RespBeanEnum.SECKILL_PRICE);
        }
        SeckillGoods seckillGoods=seckillground(goodsVo);
        return RespBean.success();
    }
    @Override
    public SeckillGoods seckillground(GoodsVo goodsVo){
        SeckillGoods seckillGoods=new SeckillGoods();
        seckillGoods.setId(goodsVo.getId());
        seckillGoods.setGoodsId(goodsVo.getId());
        seckillGoods.setSeckillPrice(goodsVo.getSeckillPrice());
        seckillGoods.setStockCount(goodsVo.getStockCount());
        seckillGoods.setStartDate(goodsVo.getStartDate());
        seckillGoods.setEndDate(goodsVo.getEndDate());
        seckillGoodsMapper.insert(seckillGoods);
        return seckillGoods;
    }
}
